home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-24 | 14.8 KB | 606 lines | [TEXT/MPS ] |
- /******************************************************************************
- ** **
- ** Module: PPTestApp.cp **
- ** **
- ** **
- ** Purpose: Test harness for PowerPlay **
- ** **
- ** Author: Jamie Osborne **
- ** **
- ** Copyright (C) 1996 Apple Computer, Inc. All rights reserved. **
- ** **
- ** **
- *****************************************************************************/
-
- #define VERBOSE 0
-
- #include "NSTestApp.h"
- #include "CPlayerWindow.h"
- #include <LGrowZone.h>
- #include <LWindow.h>
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <PPobClasses.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
- #include <LEditField.h>
- #include <iostream>
- #include <sioux.h>
- #include <stdio.h>
- #include <UModalDialogs.h>
- #include <LArrayIterator.h>
- #include <LString.h>
-
- #include <OpenTptAppleTalk.h>
-
- #include <NetSprocket.h>
-
- // put declarations for resource ids (ResIDTs) here
-
- const CommandT cmd_Advertise = 2000;
- const CommandT cmd_Join = 2001;
- const CommandT cmd_Unadvertise = 2002;
- const CommandT cmd_Start = 2003;
-
- enum {kKeyboardState = 600, kGameStart, kGameEnd};
- Boolean IsPressed(unsigned short k);
- // ===========================================================================
- // • Main Program
- // ===========================================================================
-
- void main(void)
- {
- SIOUXSettings.autocloseonquit = true;
- SIOUXSettings.asktosaveonclose = true;
- SIOUXSettings.initializeTB = false;
- SIOUXSettings.setupmenus = false;
- SIOUXSettings.standalone = false;
- // Set Debugging options
- SetDebugThrow_(debugAction_SourceDebugger);
- SetDebugSignal_(debugAction_SourceDebugger);
- InitializeHeap(3); // Initialize Memory Manager
- // Parameter is number of Master Pointer
- // blocks to allocate
-
- // Initialize standard Toolbox managers
- UQDGlobals::InitializeToolbox(&qd);
-
- new LGrowZone(20000); // Install a GrowZone function to catch
- // low memory situations.
- PPTestApp theApp; // replace this with your App type
-
- theApp.Run();
- }
-
-
- // ---------------------------------------------------------------------------
- // • CPPStarterApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Constructor
-
- PPTestApp::PPTestApp()
- {
- // Register functions to create core PowerPlant classes
- bAdvertising = false;
- bRunning = false;
- mPlayerCount = 0;
- theGame = NULL;
- RegisterAllPPClasses();
- URegistrar::RegisterClass(CPlayerWindow::class_ID, CPlayerWindow::CreatePlayerWindowStream);
- gPulseBit = 0;
-
- // Initialize our player list
- CPlayerListComparator *theComparator = new CPlayerListComparator();
- ThrowIfNULL_(theComparator);
- mPlayerList = new LArray(sizeof(PlayerListItem), theComparator, true);
- ThrowIfNULL_(mPlayerList);
-
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~PPTestApp // replace this with your App type
- // ---------------------------------------------------------------------------
- // Destructor
- //
-
- PPTestApp::~PPTestApp()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • StartUp
- // ---------------------------------------------------------------------------
- // This function lets you do something when the application starts up.
- // For example, you could issue your own new command, or respond to a system
- // oDoc (open document) event.
-
- void
- PPTestApp::StartUp()
- {
- OSStatus err;
- UInt32 choice = 0;
-
- ObeyCommand(cmd_New, nil); // EXAMPLE, create a new window
-
- // Do some quick and dirty testing
- cout << "Starting up" << endl;
- err = InitializeNetSprocket(500, 800000, 40);
- cout << "InitializeNetSprocket: " << err << endl;
-
- }
-
-
- Boolean IsPressed(unsigned short k)
- // k = any keyboard scan code, 0-127
- {
- KeyMap map;
- unsigned char km[16];
-
- GetKeys(map);
- BlockMove(map, km, 16);
-
- Boolean pressed;
- pressed = ( ( km[k>>3] >> (k & 7) ) & 1);
-
- return pressed;
- }
- int gLastTickCount = 0;
-
- void
- PPTestApp::ProcessNextEvent()
- {
- EventRecord macEvent;
- TNetMessageHeader *theMessage;
- if (IsOnDuty()) {
-
- // Calling OSEventAvail with a zero event mask will always
- // pass back a null event. However, it fills the EventRecord
- // with the information we need to set the cursor shape--
- // the mouse location in global coordinates and the state
- // of the modifier keys.
-
- ::OSEventAvail(0, &macEvent);
- AdjustCursor(macEvent);
- }
-
- // Retrieve the next event. Context switch could happen here.
-
- Boolean gotEvent = ::WaitNextEvent(everyEvent, &macEvent, 0,
- mMouseRgnH);
-
-
- Boolean SIOUXDidEvent = SIOUXHandleOneEvent(&macEvent);
- if (!SIOUXDidEvent)
- {
- SetUpdateCommandStatus(false);
-
- // Let Attachments process the event. Continue with normal
- // event dispatching unless suppressed by an Attachment.
-
- if (LAttachable::ExecuteAttachments(msg_Event, &macEvent)) {
- if (gotEvent) {
- DispatchEvent(macEvent);
- } else {
- UseIdleTime(macEvent);
- }
- }
- }
- // Repeaters get time after every event
- LPeriodical::DevoteTimeToRepeaters(macEvent);
-
- // Update status of menu items
- if (IsOnDuty() && GetUpdateCommandStatus()) {
- UpdateMenus();
- }
-
- if (theGame && bRunning /*&& IsPressed(122)*/)
- {
- UInt32 count = TickCount();
- if (count >= gLastTickCount)
- {
- TKeyState state;
-
- gLastTickCount = count + 3;
-
- ClearNetMessageHeader((TNetMessageHeader *)&state);
- state.h.what = kKeyboardState;
- state.h.from = GetMyPlayerID(theGame);
- state.h.to = 0;
- state.h.messageLen = sizeof(TKeyState);
-
- state.keyMap[0] = IsPressed(0x7e) ? 1 : 0;
- state.keyMap[1] = IsPressed(0x7d) ? 1 : 0;
- state.keyMap[2] = IsPressed(0x7c) ? 1 : 0;
- state.keyMap[3] = IsPressed(0x7b) ? 1 : 0;
- state.pulseBit = gPulseBit;
- gPulseBit = (gPulseBit == 1) ? 0 : 1;
-
- // if (state.keyMap[0] != 0)
- SendNetMessage(theGame, (TNetMessageHeader *) &state, kSelfSend);
- }
- }
-
- if (theGame)
- {
- while ((theMessage = GetNextNetMessage(theGame)) != NULL)
- {
- HandleGameEvent(theMessage);
- ReleaseNetMessage(theGame, (TNetMessageHeader *)theMessage);
- }
- }
-
- }
-
-
- void PPTestApp::HandleGameEvent(TNetMessageHeader *inEvent)
- {
- #if VERBOSE
- cout << "Handling event:";
- cout << "\twhat = " << inEvent->what << endl;
- cout << "\twhen = " << inEvent->when << endl;
- cout << "\tfrom = " << inEvent->from << endl;
- cout << "\tto = " << inEvent->to << endl;
- cout << "\tid = " << inEvent->id << endl;
- cout << "\tdataLen = " << inEvent->dataLen << endl;
- cout << "\tcookie = " << inEvent->cookie << endl;
- #endif
- switch (inEvent->what)
- {
- case kNSJoinApproved:
- break;
- case kNSJoinDenied:
- break;
- case kNSPlayerJoined:
- HandlePlayerJoined((TPlayerJoinedMessage *)inEvent);
- break;
- case kNSPlayerLeft:
- HandlePlayerLeft((TPlayerLeftMessage *)inEvent);
- break;
- case kKeyboardState:
- HandleKeyboardState((TKeyState *)inEvent);
- break;
- case kGameStart:
- HandleGameStart(inEvent);
- break;
- case kGameEnd:
- HandleGameEnd(inEvent);
- break;
- default:
- cout << "Unhandled game event:" << inEvent->what << endl;
- break;
- }
- }
-
- void PPTestApp::HandlePlayerJoined(TPlayerJoinedMessage *inEvent)
- {
- PlayerListItem item;
- TNetMessageHeader theMessage;
-
- cout << "In HandlePlayerJoined" << endl;
-
- if (bRunning)
- {
- ClearNetMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = inEvent->playerInfo.id;
- theMessage.messageLen = sizeof(TNetMessageHeader);
-
- SendNetMessage(theGame, &theMessage, kSelfSend);
-
- LWindow *window = LWindow::CreateWindow(1002, this);
- window->SetDescriptor(inEvent->playerInfo.name);
- window->Show();
- item.player = inEvent->playerInfo.id;
- item.window = window;
- mPlayerList->InsertItemsAt(1, 0, &item);
- }
- mPlayerCount++;
-
- }
-
- void PPTestApp::HandlePlayerLeft(TPlayerLeftMessage *inEvent)
- {
- cout << "In HandlePlayerLeft" << endl;
-
- ArrayIndexT index = mPlayerList->FetchIndexOfKey(&inEvent->playerID);
- PlayerListItem item;
- mPlayerList->FetchItemAt(index, &item);
- item.window->DoClose();
-
- mPlayerCount--;
- }
-
- void PPTestApp::HandleGameStart(TNetMessageHeader *inEvent)
- {
- UInt32 i;
- LWindow *window;
- bRunning = true;
- PlayerListItem item;
- LStr255 title;
- OSStatus err;
-
- TPlayersPtr thePlayers;
- TPlayerInfoPtr playerInfo;
-
- err = GetPlayers(theGame, &thePlayers);
- if (err == kNSNoError)
- {
- for (i = 0; i < thePlayers->count; i++)
- {
- playerInfo = thePlayers->playerInfo[i];
- window = LWindow::CreateWindow(1002, this);
- window->SetDescriptor(playerInfo->name);
- window->Show();
- item.player = playerInfo->id;
- item.window = window;
-
- mPlayerList->InsertItemsAt(1, 0, &item);
-
- }
-
- ReleasePlayers(theGame, thePlayers);
- }
- else
- cout << "GetPlayers returned " << err << endl;
- }
-
- void PPTestApp::HandleGamePause(TNetMessageHeader *inEvent)
- {
- cout << "In HandleGamePause" << endl;
- }
-
- void PPTestApp::HandleGameEnd(TNetMessageHeader *inEvent)
- {
- cout << "In HandleGameEnd" << endl;
- PlayerListItem item;
- LArrayIterator iter(*mPlayerList);
-
- while(iter.Next(&item))
- {
- item.window->DoClose();
- mPlayerList->Remove(&item);
- }
-
- bRunning = false;
- OSStatus err = DeleteGame(theGame, 0);
- theGame = NULL;
- }
-
- void PPTestApp::HandleKeyboardState(TKeyState *inEvent)
- {
- PlayerListItem info;
- ArrayIndexT index;
-
- index = mPlayerList->FetchIndexOfKey(&inEvent->h.from);
- #if DEBUG
- if (index == LArray::index_Bad)
- cout << "Got a bad player list index in HandleKeyboardState" << endl;
- #endif
- if (mPlayerList->FetchItemAt(index, &info))
- {
- CPlayerWindow *theWindow = (CPlayerWindow *)info.window;
-
- if (theWindow)
- {
- theWindow->mUp = inEvent->keyMap[0];
- theWindow->mDown = inEvent->keyMap[1];
- theWindow->mLeft = inEvent->keyMap[2];
- theWindow->mRight = inEvent->keyMap[3];
- theWindow->mPulse = inEvent->pulseBit;
-
- theWindow->Refresh();
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- // ---------------------------------------------------------------------------
- // Respond to commands
-
- Boolean
- PPTestApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
- OSErr err;
- Str255 name;
- Str255 password;
- Str255 gameName;
- UInt16 port = 3333;
- TNetMessageHeader theMessage;
-
- switch (inCommand) {
-
- // Deal with command messages (defined in PP_Messages.h).
- // Any that you don't handle will be passed to LApplication
-
- case cmd_Advertise:
- {
- UInt32 protocols = 0;
- LString::CopyPStr("\pTest1", gameName);
- LString::CopyPStr("\pbarney", password);
- LString::CopyPStr("\pDarmok", name);
-
- protocols = DoModalHostDialog("\pChoose your poison:", &port, gameName, name, password);
-
- cout << "DoModalAdvertiseDialog returned a protocol list of " << protocols << endl;
-
- if (protocols == 0)
- break;
-
- err = NewGame(&theGame, 12, gameName, password, name, 0, kNSClientServer, 0);
- if (err != kNSNoError)
- {
- cout << "NewGame returned an error! " << err << endl;
- return true;
- }
- mPlayerCount++;
- if (protocols & kAppleTalk)
- {
- err = HostGameOnAppleTalk(theGame, "\pbandersnatch", 0);
- cout << "HostGame on AppleTalk (expect 0): " << err << endl;
- }
- if (protocols & kTCPIP)
- {
- err = HostGameOnTCPIP(theGame, &port, 0);
- cout << "HostGame on TCP/IP (expect 0): " << err << endl;
- }
- if (protocols & kSerial)
- {
- cout << "Serial Not implemented" << err << endl;
- }
- bAdvertising = true;
- bHost = true;
- }
- break;
- case cmd_Unadvertise:
- if (bAdvertising)
- {
- err = StopHostingAppleTalk(theGame);
- cout << "StopHostingGame game (expect 0): " << err << endl;
- if (err == noErr)
- bAdvertising = false;
- }
- break;
- case cmd_Join:
- {
- TGameAddressRef theAddress;
- LString::CopyPStr("\pDarmok", name);
- LString::CopyPStr("\pbarney", password);
-
- theAddress = DoModalJoinDialog("\pbandersnatch", "\pGame servers:", name, password);
- cout << "DoModalJoinWithName returned an address ref of " << theAddress << endl;
- if (theAddress != NULL)
- {
- err = JoinGame(&theGame, theAddress, name, password, 0, 0, NULL,0);
- cout << "JoinGame returned err: " << err << endl;
- ReleaseGameAddressRef(theAddress);
- }
- bHost = false;
- bAdvertising = false;
- }
- break;
- case cmd_Start:
- ClearNetMessageHeader(&theMessage);
- theMessage.what = kGameStart;
- theMessage.to = 0;
- theMessage.messageLen = sizeof(TNetMessageHeader);
-
- SendNetMessage(theGame, &theMessage, kSelfSend);
- // bRunning = true;
-
- break;
- case cmd_Quit:
- if (theGame)
- {
- if (bHost)
- {
- ClearNetMessageHeader(&theMessage);
- theMessage.what = kGameEnd;
- theMessage.to = 0;
- theMessage.messageLen = sizeof(TNetMessageHeader);
-
- SendNetMessage(theGame, &theMessage, 0);
- }
- DeleteGame(theGame, kNSForceTerminateGame);
- theGame = NULL;
- }
- bRunning = false;
- LApplication::ObeyCommand(inCommand, ioParam);
- default:
- cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
- // ---------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------
- // This function enables menu commands.
- //
-
- void
- PPTestApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName)
- {
-
- switch (inCommand) {
-
- // Return menu item status according to command messages.
- // Any that you don't handle will be passed to LApplication
-
- case cmd_Advertise:
- outEnabled = true;
- break;
- case cmd_Unadvertise:
- if (bAdvertising == true)
- outEnabled = true;
- else
- outEnabled = false;
- break;
- case cmd_Join:
- outEnabled = true;
- break;
- case cmd_Start:
- outEnabled = true;
- break;
- default:
- LApplication::FindCommandStatus(inCommand, outEnabled,
- outUsesMark, outMark, outName);
- break;
- }
- }
-
-
- Int32
- CPlayerListComparator::Compare(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) - (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualTo(
- const void* inItemOne,
- const void* inItemTwo,
- Uint32 /* inSizeOne */,
- Uint32 /* inSizeTwo */) const
- {
- return ( (((PlayerListItem *)inItemOne)->player) == (((PlayerListItem *)inItemTwo)->player) );
- }
-
-
- Int32
- CPlayerListComparator::CompareToKey(
- const void* inItem ,
- Uint32 /* inSize */,
- const void* inKey ) const
- {
- return ( (((PlayerListItem *)inItem)->player) - (*(TPlayerID *)inKey) );
- }
-
-
- Boolean
- CPlayerListComparator::IsEqualToKey(
- const void* inItem,
- Uint32 inSize,
- const void* inKey) const
- {
- return ( (((PlayerListItem *)inItem)->player) == (*(TPlayerID *)inKey) );
- }
-